<template>
  <div class="hyydxq">
    <!-- 羊奶销售 -->

    <!-- 选择日期 开始 -->
    <div class="listSearch">
      <div>
        <!-- <img src="@/assets/img/sou.png" alt="" class="sou">
        <van-search type="text" v-model="params.salesBatchNo" @search="onSearch"  placeholder="请输入关键词进行搜索" />
        <img src="@/assets/img/cha.png" alt="" @click="params.salesBatchNo = null" class="cha"> -->
        <van-search
          clearable
          background="transparent"
          v-model="params.salesBatchNo"
          @search="onSearch"
          class="b-b-2"
          placeholder="请输入关键词进行搜索"
        />
      </div>
      <img src="@/assets/img/rili.png" style="margin-left: 20px" alt="" class="rili" @click="dateShow = true" />
    </div>
    <van-calendar v-model="dateShow" type="range" @confirm="onConfirm" color="#37B5B5" />
    <!-- 选择日期 结束 -->
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
      <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
        <van-cell v-for="(item, key) in list" :key="key" class="approvalItem content">
          <div @click="detail(item)">
            <div class="flex-between font-14 line-height-3">
              <div class="col-333333 font-bold-500 font-15 m-r-10">{{ item.salesBatchNo }}</div>
            </div>
            <div class="flex-between font-14 line-height-3">
              <div class="col-666666 line-text-out text-right">销售日期：{{ item.salesDate }}</div>
              <div class="col-666666 m-r-10 line-text-out">生鲜乳来源（家）：{{ item.sourceNumber }}</div>
            </div>
            <div class="flex-between font-13 line-height-3">
              <div class="col-666666 m-r-10">生鲜乳总数量（kg）：{{ item.freshMilkTotal }}</div>
            </div>
            <div class="flex-between font-13 line-height-3">
              <div class="col-666666 m-r-10" v-if="item.subjectEnterpriseInfo">
                销售去向：{{ item.subjectEnterpriseInfo.enterpriseName }}
              </div>
            </div>
            <div class="flex-between font-14 line-height-3">
              <div class="col-666666 line-text-out text-right">创建人：{{ item.createdByName }}</div>
              <div class="col-666666 m-r-10 line-text-out">创建时间：{{ item.createTime }}</div>
            </div>
          </div>
          <div class="flex-end m-t-10">
            <van-button class="btn m-r-15" @click="scanDetail(item)">查看二维码</van-button>
          </div>
        </van-cell>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
/**
 * 羊奶销售
 **/
import { getValue, setValue } from '@/utils/auth'
import {
  Toast,
  Cell,
  CellGroup,
  Popup,
  Picker,
  DatetimePicker,
  PullRefresh,
  CheckboxGroup,
  List,
  Search,
  Button,
  Calendar
} from 'vant'
import { getHandle, postHandle } from '@/api'
import dayjs from 'dayjs'

export default {
  components: {
    [Cell.name]: Cell,
    [CellGroup.name]: CellGroup,
    [Popup.name]: Popup,
    [Picker.name]: Picker,
    [DatetimePicker.name]: DatetimePicker,
    [PullRefresh.name]: PullRefresh,
    [CheckboxGroup.name]: CheckboxGroup,
    [Button.name]: Button,
    [Toast.name]: Toast,
    [Calendar.name]: Calendar,
    [Search.name]: Search,
    [List.name]: List
  },
  data() {
    return {
      keyword: '',
      refreshing: false,
      currentDate: new Date(),
      list: [],
      loading: false,
      finished: false,
      params: {
        page: 1,
        limit: 10,
        salesBatchNo: null,
        tenantCode: getValue('tenantCode'),
        type: 1,
        dateRange: null,
        whereaboutsEnterpriseId: getValue('subjectInfoId')
      },
      // params: {
      //   page: 1,
      //   perPage: 5,
      //   startDate: null, // 开始时间
      //   endDate: null, // 结束时间
      //   value: null // 关键字搜索
      //
      // },
      showPicker1: false,
      flag: null,
      dateShow: false // 日期弹出框
    }
  },
  watch: {},
  created() {},
  mounted() {},
  methods: {
    onConfirm(date) {
      const [start, end] = date
      this.dateShow = false
      this.params.dateRange = [dayjs(start).format('YYYY-MM-DD'), dayjs(end).format('YYYY-MM-DD')].join(',')
      this.list = []

      this.params.page = 1
      this.finished = false

      // 重新加载数据
      // 将 loading 设置为 true，表示处于加载状态
      this.loading = true
      this.onLoad()
    },
    // 卡片列表
    onRefresh() {
      this.params.page = 1
      this.finished = false
      this.loading = true
      this.onLoad()
    },
    onSearch() {
      this.params.page = 1
      this.list = []
      this.finished = false
      this.loading = true
      this.onRefresh()
    },
    onLoad() {
      getHandle('/agri/api/yzbzh/saleInfo/queryListPage', this.params).then(res => {
        if (this.refreshing) {
          this.list = []
          this.refreshing = false
        }
        this.list = this.list.concat(res.data.rows)
        this.loading = false
        console.log(this.list)
        if (this.list.length >= res.data.count) {
          this.finished = true
        } else {
          this.params.page = this.params.page + 1
        }
      })
    },
    detail(item) {
      setValue('sourceInfo', JSON.stringify(item))
      this.$bridge.call('salesTraceabilityCodeDetails')
    },
    scanDetail(item) {
      setValue('milKId', item.id)
      this.$bridge.call('qrCodeView')
    }
  }
}
</script>

<style lang="scss" scoped>
// 选择日期开始
.listSearch {
  display: flex;
  align-items: center;
  padding: 0 15px;
  box-sizing: border-box;
  justify-content: space-between;
  padding-top: 10px;
  div {
    width: 310px;
    height: 30px;
    position: relative;
    background-color: #fff;
    input {
      height: 100%;
      box-sizing: border-box;
      position: absolute;
      left: 28px;
      width: calc(100% - 50px);
    }
    img {
      width: 12px;
      height: 12px;
      position: absolute;
      top: 50%;
      transform: translate(0, -50%);
    }
    .sou {
      left: 10px;
    }
    .cha {
      right: 10px;
    }
  }
  .rili {
    width: 20px;
    height: 20px;
  }
}
// 选择日期结束
.topCeiling {
  position: fixed;
  top: 0;
  z-index: 1;
}
.topDate {
  width: 80%;
  margin: auto;
  display: flex;
  justify-content: space-between;
  > p {
    font-size: 15px;
    &:first-child {
      width: 40%;
    }
    &:last-child {
      width: 40%;
    }
  }
}
.btn {
  height: 24px;
  width: 100px;
  background: #37b5b5;
  color: #ffffff;
  white-space: nowrap;
  text-align: center;
  font-size: 13px;
  border-radius: 5px;
}
.van-field__control {
  border: 0 !important;
}
.content {
  //   border-bottom: 1px solid #eee;
  padding: 10px 15px;
}
</style>
